package com.mlamp.nanchang.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mlamp.nanchang.entity.FaceWarning;
import com.mlamp.nanchang.entity.RlsbBak;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class ScheduleTask {
	private static final Logger log = LoggerFactory.getLogger(ScheduleTask.class);
	@Autowired
	private FaceWarningService service;
	@Autowired
	private RlsbBakService rlsbBakService;

	//单位毫秒
	//@Scheduled(cron = "40 * * * * ?") //每隔40秒
	public void scheduleFaceWarning() {
		int pageNo = 0, pageSize = 500;
		long total = 0;

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.000+08:00");
		long now = System.currentTimeMillis();
		String starttime = sdf.format(new Date(now - 1000*60*60*2));
		String endtime = sdf.format(new Date(now));

		do {
			Page<FaceWarning> page = service.pagingFaceWarning(++pageNo, pageSize, starttime, endtime);
			if(page != null) {
				service.saveFaceWarning(page.getRecords());
				total = page.getTotal();
			}
		}while (total > pageNo*pageSize);

	}

//	@Scheduled(fixedRate = 1000 * 60 * 60 * 24)
	@Scheduled(cron = "0 0 0/4 * * ?") //每隔4个小时
	public void scheduleRlsbBak() {
		int pageNo = 0, pageSize = 500;
		long total = 0;
		System.out.println("------------请求视频数据");
		int ct = 1;
		log.debug("第1页");
		Page<RlsbBak> page = rlsbBakService.pagingCameras(++pageNo, pageSize);
		if(page != null) {
			total = page.getTotal();
			log.info("=================总记录数："+total);
			log.info("=================总记录数："+total);
			log.info("=================总记录数："+total);
			log.info("=================总记录数："+total);
			log.info("=================总记录数："+total);
			log.info("=================总记录数："+total);
			int totalPage = (int) ((total - 1) / pageSize + 1);
			for (int i = 0; i <= totalPage; i++) {
				rlsbBakService.pagingAndSave(++pageNo, pageSize);
				log.info("第"+ pageNo +"页");
			}
		}
		log.debug("拉取结束!");
	}

}
